import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/system_utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:get/get.dart';
import 'config/config.dart';

void main() {
  run();
  Config.init(Env.RELEASE);
}

void run() {
  //设置竖屏
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  //bug日志
  FlutterBugly.postCatchedException(() {
    runApp(MyApp());
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(375, 667),
      builder: () {
        return GetMaterialApp(
          showPerformanceOverlay: false,
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colours.theme,
            canvasColor: Colors.white,
          ),
          navigatorKey: Routes.navigatorKey,
          initialRoute: Routes.splash,
          getPages: Routes.getPages,
          defaultTransition: Transition.rightToLeft,
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
            RefreshLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale("zh", "CH"),
            const Locale("en", "US"),
          ],
          locale: Locale("zh"),
          builder: (context, widget) {
            return RefreshConfiguration(
              headerBuilder: () => ClassicHeader(),
              footerBuilder: () => ClassicFooter(),
              hideFooterWhenNotFull: true,
              child: MediaQuery(
                data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                child: GestureDetector(
                  onTap: () {
                    SystemUtils.closeKeyboard(context);
                  },
                  child: widget,
                ),
              ),
            );
          },
        );
      },
    );
  }
}
